package com.blog.controller;

import com.alibaba.fastjson.JSON;
import com.blog.common.Cons;
import com.blog.common.ReturnResult;
import com.blog.model.StatisticalModel;
import com.blog.pojo.User;
import com.blog.service.ArticleService;
import com.blog.service.UserService;
import com.blog.util.Assert;
import com.blog.util.DateUtil;
import com.blog.util.MD5Util;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @author jiafujun
 * 2020-05-13 10:15:55
 **/
@Api("用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private ArticleService articleService;

    @Value("${system.admin}")
    private String admin;

    @ApiOperation(value = "用户登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ReturnResult login(HttpServletRequest request, HttpServletResponse response, String userName, String password) {
        Assert.notNull(userName, "用户名不能为空");
        Assert.notNull(password, "密码不能为空");

        User user = userService.getByName(userName);
        if (user == null) {
            return ReturnResult.build(Cons.RESULT_ERR, "用户名或者密码错误");
        }
        String md5 = MD5Util.MD5(password);
        if (!user.getPassword().equals(md5)) {
            return ReturnResult.build(Cons.RESULT_ERR, "用户名或者密码错误");
        }
        if (user.getStatus() == 1) {
            return ReturnResult.build(Cons.RESULT_ERR, "账号异常，请联系管理员解决。");
        }
        HttpSession session = request.getSession();
        if (user.getUserName().equals(admin)) {
            session.setAttribute("isAdmin", true);
        }
        session.setAttribute("user", user);
        return ReturnResult.ok();
    }

    @ApiOperation(value = "退出登录")
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().removeAttribute("isAdmin");
        request.getSession().removeAttribute("user");
        response.sendRedirect("/blog/ad/login");
    }

    @ApiOperation(value = "添加用户")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ReturnResult save(User user) {
        userService.save(user);
        return ReturnResult.ok();
    }

    @ApiOperation(value = "用户列表")
    @RequestMapping(value = "/list")
    public ReturnResult list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {


        PageInfo<User> list = userService.pageList(null, page, limit);
        return ReturnResult.ok(list);
    }

    @ApiOperation(value = "删除用户")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ReturnResult del(@RequestParam(value = "id") Integer id) {
        userService.del(id);
        articleService.delByUserId(id);
        return ReturnResult.ok();
    }

    @RequestMapping(value = "/statisticalData", method = RequestMethod.POST)
    public ReturnResult statisticalData(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        StatisticalModel model = articleService.statisticalData(user.getId());
        return ReturnResult.ok(model);
    }


    /**
     * 首页统计中心
     *
     * @return
     */
    @RequestMapping(value = "/getBaseData", method = RequestMethod.POST)
    public ReturnResult getBaseData(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        StatisticalModel map = articleService.statisticalData(user.getId());
        return ReturnResult.ok(map);
    }

    /**
     * 修改密码
     *
     * @param request
     * @param oldPass
     * @param newPass
     * @return
     */
    @RequestMapping(value = "/pass", method = RequestMethod.POST)
    public ReturnResult pass(HttpServletRequest request, String oldPass, String newPass) {
        User user = (User) request.getSession().getAttribute("user");
        User userinfo = userService.selectByPrimaryKey(user.getId());
        String old = MD5Util.MD5(oldPass);
        if (!userinfo.getPassword().equals(old)) {
            return ReturnResult.build("原密码密码错误！");
        }
        if (newPass.length() < 6) {
            return ReturnResult.build("新密码长度不能低于6位");
        }
        if (old.equals(MD5Util.MD5(newPass))) {
            return ReturnResult.build("新密码不能与旧密码相同！");
        }
        User up = new User();
        up.setId(user.getId());
        up.setPassword(MD5Util.MD5(newPass));
        userService.update(up);
        return ReturnResult.ok();
    }


    @RequestMapping(value = "/getUser", method = RequestMethod.POST)
    public ReturnResult getUser(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        return ReturnResult.ok(user);
    }
}
